<template>
	<view>

		<tui-list-view>
			<tui-list-cell :arrow="true" @click="goToUserInfo">
				查看用户信息
			</tui-list-cell>
		</tui-list-view>

		<tui-list-view v-if="topic.t_msgType=='group'">
			<tui-list-cell :arrow="true">
				小组成员
			</tui-list-cell>
			<tui-list-cell :hover="false">
				请稍等...正在加载
			</tui-list-cell>
		</tui-list-view>

		<tui-list-view>
			<kmlc-form-switch showType="edit" :emData="pageData.TopSetting" @on-change="setIsTop"></kmlc-form-switch>
		</tui-list-view>

		<tui-list-view v-if="topic.t_msgType=='group'">
			<kmlc-form-input showType="edit" :emData="pageData.GroupName"></kmlc-form-input>
		</tui-list-view>

		<tui-list-view>
			<tui-list-cell :arrow="true" v-if="topic.t_msgType=='group'">
				查看小组二维码
			</tui-list-cell>
		</tui-list-view>

		<tui-list-view>
			<tui-list-cell :arrow="true" @click="clearMessages">
				清空聊天记录
			</tui-list-cell>
			<tui-list-cell :arrow="true" @click="removeTopic">
				移除会话
			</tui-list-cell>
		</tui-list-view>

		<view v-if="topic.t_msgType=='group'">
			<tui-list-view v-if="state.IsMeCreateGroup">
				<tui-list-cell :arrow="true" @click="dismissGroup">
					解算小组
				</tui-list-cell>
			</tui-list-view>

			<tui-list-view v-else>
				<tui-list-cell :arrow="true" @click="quitGroup">
					退出小组
				</tui-list-cell>
			</tui-list-view>
		</view>




	</view>
</template>

<script>
	import {
		user,
		DB,
		tools,
		PushMsg
	} from '@/common/framework';
	export default {
		data() {
			return {
				curUserInfo: {},
				topic: {},
				pushMsg: PushMsg.init(),
				state: {
					isLoadingPage: true,
					isLoadingMore: false,
					IsMeCreateGroup: false,
					IsChangeUserItems: false
				},
				pageData: {
					isTop: false,
					isNotification: false,
					unreadMessageCount: 0,
					GroupUserItems: [],
					GroupUser: {
						disabled: true,
						type: 'm-select-person-two',
						label: '小组成员',
						key: 'groupUser',
						state: {
							isLoading: true,
						},
						required: true,
						disDelAble: true, //不能被删除
						value: []
					},
					GroupName: {
						readonly: true,
						type: 'kmlc-form-input',
						label: '小组名称',
						key: 'groupName',
						inputType: 'text',
						required: true,
						value: ''
					},
					TopSetting: {
						type: 'kmlc-form-switch',
						label: '是否置顶',
						key: 'isTop',
						value: false
					}
				}
			}
		},
		async onLoad(option) {
			this.topic = JSON.parse(decodeURIComponent(option.topic));
			await this.init();
			this.curUserInfo = user.getUserInfo();
			setTimeout(async () => {

				switch (this.topic.t_msgType) {
					case "system":
						break;
					case "user":
						break;
					case "group":
						this.getGroupInfo();
						this.getGroupUser();
						break;
				}
			}, 300);

		},
		methods: {
			async init() {
				this.topic = await this.pushMsg.getTopicInfo(this.topic.t_id);
				console.log(this.topic);
				this.pageData.TopSetting.value = this.topic.t_isTop == 0 ? false : true;
			},
			
			goToUserInfo(){
				uni.navigateTo({
					url: '/pages/addressBook/detail/detail?id=' +this.topic.t_senderId
				})
			},

			getGroupInfo() {
				this.pushMsg.getGroupInfo(this.topic.t_senderId).then((ret) => {
					if (ret && ret.Data) {
						let groupInfo = ret.Data;
						this.pageData.GroupName.value = groupInfo.name;
						if (groupInfo.createAlias == this.curUserInfo.userId) {
							this.state.IsMeCreateGroup = true;
							this.pageData.GroupUser.readonly = false;
							this.pageData.GroupName.disabled = false;
						}
					}
				}).catch(err => {
					console.error(err);
					tools.toast('操作失败');
				})


			},

			getGroupUser() {
				this.pageData.GroupUser.state.isLoading = true;
				this.pushMsg.getGroupMember(this.topic.t_senderId).then((ret) => {
					this.pageData.GroupUser.state.isLoading = false;
					if (ret && ret.Result == 1) {
						this.pageData.GroupUser.value = [];
						ret.Data.map((it) => {
							if (it) {
								this.pageData.GroupUser.value.push({
									id: it.alias,
									realname: it.userName,
									pic_path: it.headerPic.replace('.100x100.png', '')
								});
							}
						})
						this.pageData.GroupUserItems = JSON.parse(JSON.stringify(this.pageData.GroupUser.value));
						this.$set(this.pageData.GroupUser, this.pageData.GroupUser);
					}
				}).catch(err => {
					console.error(err);
					tools.toast('操作失败');
				})
			},

			//设置是否置顶
			setIsTop(data) {
				let state = data.value?1:0;
				console.log(state);
				this.pushMsg.setTopicIsTop(this.topic.t_id, state).then((ret) => {
					tools.toast('设置成功');
				}).catch((err) => {
					console.error(err);
					tools.toast('设置失败');
				})
			},

			//移除会话
			removeTopic(cb) {
				this.pushMsg.removeTopic(this.topic.t_id).then(() => {
					console.log(11);
					uni.$emit('removeConversation', {
						targetId: this.topic.t_id
					});
					uni.$emit('clearMessagesUnreadStatus', {});

					if (typeof cb == "function") {
						cb();
					} else {
						tools.toast('会话移除成功');
						uni.switchTab({
							url: '../index/index'
						});
					}
				}).catch((err) => {
					tools.toast('操作失败');
					console.error(err);
				});
			},
			//清空消息
			clearMessages() {
				this.pushMsg.clearDialogue(this.topic.t_id).then(() => {
					uni.$emit('clearMessagesUnreadStatus', {});
					tools.toast('消息已经清空');
				})
			},

			dismissGroup() {
				try {
					uni.showModal({
						"title": '提示',
						content: "确定要解散该小组吗？",
						confirmText: "确定",
						cancelText: "取消",
						cancelColor: '#0062CC',
						confirmColor: '#555',
						success: async () => {
							uni.showLoading({
								title: 'loading'
							});
							let ret = await this.pushMsg.removeGroup(this.topic.t_senderId, this.curUserInfo.uerId);
							if (ret && ret.Result == 1) {
								this.removeTopic(() => {
									uni.hideLoading();
									tools.toast('操作成功');
									setTimeout(() => {
										uni.switchTab({
											url: '../index/index'
										});
									}, 300);
								});

							}
						}
					});
				} catch (err) {
					tools.toast('操作失败');
					console.error(err);
				}
			},

			//退出小组
			quitGroup() {
				try {
					uni.showModal({
						"title": '提示',
						content: "确定要退出该小组吗？",
						confirmText: "确定",
						cancelText: "取消",
						cancelColor: '#0062CC',
						confirmColor: '#555',
						success: async () => {
							uni.showLoading({
								title: 'loading'
							});
							let ret = await this.pushMsg.leaveGroup(this.topic.t_senderId, this.curUserInfo.uerId);
							if (ret && ret.Result == 1) {
								this.removeTopic(() => {
									uni.hideLoading();
									tools.toast('操作成功');
									setTimeout(() => {
										uni.switchTab({
											url: '../index/index'
										});
									}, 300);
								});

							}
						}
					});
				} catch (err) {
					tools.toast('操作失败');
					console.error(err);
				}
			},
		}

	}
</script>

<style>
</style>
